<?php
/**
 * Created by PhpStorm.
 * User: Zhoujinda
 * Date: 2016/8/3
 * Time: 11:00
 */
namespace Webview\Controller;

use Admin\Api\DatatreeApi;
use Admin\Api\ViewPageItemApi;
use Common\Controller\BaseController;
use Shop\Api\ProductApi;
use Shop\Api\ProductGroupApi;
use Shop\Api\ProductPackageApi;

class ViewPageController extends BaseController{

    public function index(){

        $block_id = I('block',-1);
        $banner_index = I('banner',-1,'int');

        $result = apiCall(ViewPageItemApi::GET_INFO,[['id'=>$block_id]]);
        if($result['status']){
            if(is_null($result['info']))exit('no block');

            $info = $result['info'];
            $data = json_decode($info['data'],true);
            $banners = $data['banners'];
            $page_id = $info['page_id'];
            if(is_null($banners))exit('no banners');

            $view = 'error';
            $banner = [];
            $size = 10;
            if($info['dt_item_type']==6108){
                //首页四格
                if(!isset($banners[$banner_index]))exit('error banner');
                $banner = $banners[$banner_index];
                switch($banner_index){
                    case 0:
                        //保税区
                        $view = 'baoshuiqu';
                        $this->assignTitle('保税区');
                        break;
                    case 1:
                        //搭配购
                        $view = 'dapeigou';
                        $this->assignTitle('搭配购');
                        break;
                    case 2:
                        //特卖汇
                        $view = 'temaihui';
                        $this->assignTitle('特卖汇');
                        $size = 9;
                        break;
                    case 3:
                        //限量购
                        $view = 'xianlianggou';
                        $this->assignTitle('限量购');
                        $size = 6;
                        break;
                    default:
                        break;
                }

            }else{
                $types = [
                    6109,//首页单图
                    6110,//首页六格
                    6111,//首页一四图
                    6112,//首页大图带产品 分类一图三产品
                ];

                if(in_array($info['dt_item_type'],$types)){
                    if(!isset($banners[$banner_index]))exit('error banner');
                    $banner = $banners[$banner_index];
                    $view = 'splist';
                    $this->assignTitle('商品列表');
                }
            }
            if($view == 'dapeigou'){
                //搭配购
                //搭配购商品列表 套餐dt_type == 1;
                $map = ['dt_type' => 1];
                $page = ['curpage'=>1, 'size'=>10];
                $result = apiCall(ProductPackageApi::QUERY_WITH_COUNT,[$map,$page,false]);
                if($result['status']){
                    //查询套餐信息
                    $info = $result['info'];
                    foreach($info['list'] as &$val){
                        $result = apiCall(ProductPackageApi::PACKAGE_DETAIL,[$val['id']]);
                        if($result['status']){
                            $val['detail'] = $result['info'];
                        }
                    }
                    $this->assign('package_info',$info);
                }else{
                    exit($result['info']);
                }


            }
            elseif($view!='error'){
                $PICTURE_REMOTE_URL = C('PICTURE_REMOTE_URL');
                //分组主图
                if(isset($banner['group_main_banner'])){
                    $main_banner = $PICTURE_REMOTE_URL.$banner['group_main_banner']['id'];
                    $this->assign('main_banner',$main_banner);
                }

                //取活动时间
                if(isset($banner['group_main_time'])){
                    $main_time = $banner['group_main_time'];
                    $this->assign('main_time',$main_time);
                }

                //取商品列表
                $group_product_list = $banner['group_product_list'];
                if(is_null($group_product_list))exit('error group_product_list');

                //获取group_id
                //查找页面商品分组 data id;
                $map = [
                    'name' => '页面商品分组',
                    'level' => 0
                ];
                $result = apiCall(DatatreeApi::GET_INFO,[$map]);
                if($result['status']){
                    if(is_null($result['info'])){
                        exit('no group_dt');
                    }
                    $dt_id = $result['info']['id'];
                }else{
                    exit('error 3');
                }
                $group_name = "页面商品分组.$page_id.$block_id.$banner_index";
                $map = [
                    'name' => $group_name,
                    'parentid' => $dt_id,
                    'level' => 1
                ];
                $result = apiCall(DatatreeApi::GET_INFO,[$map]);
                if($result['status']){
                    if(!is_null($result['info'])){
                        $group_id = $result['info']['id'];
                        $this->assign('group_id',$group_id);
                    }else{
                        exit('no group');
                    }
                }else{
                    exit('error 4');
                }

                $order = I('order',11);
                if(!in_array($order,[10,11,12,13,14]))$order = 10;
                if($order==10){
                    //默认
                    $order = false;
                }elseif($order==11){
                    //人气
                    $order = 'view_cnt desc';

                }elseif($order==12){
                    //销量
                    $order = 'total_sales desc';
                }elseif($order==13){
                    //价格从高到低
                    $order = 'price desc';
                }elseif($order==14){
                    //价格从低到高
                    $order = 'price asc';
                }elseif($order==15){
                    //更新时间
                    $order = 'updatetime desc';
                }
                $price = I('price');
                $map = [];
                if($price==10){
                    $map['price'] = [['egt',0],['lt',500]];
                }elseif($price==11){
                    $map['price'] = [['egt',500],['lt',1000]];
                }elseif($price==12){
                    $map['price'] = ['egt',1000];
                }

                $result = $this->_queryProduct($group_product_list['p_ids'],1,$size,$group_id,$order,$map);

                if($result['status']){
                    $this->assign('product_info',$result['info']);
                    $this->assign('p_ids',json_encode($group_product_list['p_ids']));
                }else{
                    exit('error 2');
                }
            }

        }else{
            exit('error');
        }

        $this->display('ViewPage:'.$view);

    }

    private function _queryProduct($pids=[],$curpage=1,$size=10,$group_id=false,$order=false,$_map=[]){
//        $map = ['name'=>'萌屋商城'];
//        $store_id = 0;
//        $result = apiCall("Shop/Store/getInfo",array($map));
//        if($result['status']){
//            $store_id = $result['info']['id'];
//        }
//        if(empty($store_id)){
//            $this->ajaxReturn(['status'=>false,'info'=>'请先创建店铺!']);
//        }


        $ids = $pids;
        if(empty($ids))  return ['status'=>true,'info'=>['count'=>0,'list'=>[]]];
        $map = [
            'store_id' => 1,
            'id' => ['in',$ids],
            'onshelf' => 1,
            'status' => 1
        ];

        $map = array_merge($_map,$map);

        $field = ['product_id','name','img','price','dt_origin_country','updatetime'];
        $page = ['curpage'=>$curpage,'size'=>$size];
        $result = apiCall(ProductApi::QUERY_WITH_COUNT,[$map,$page,$order,false,$field]);
        if($result['status']){
            $list = $result['info']['list'];
            $count = $result['info']['count'];
            $PICTURE_REMOTE_URL = C('PICTURE_REMOTE_URL');

            //获取国别
            $map = [
                'parentid' => 35
            ];
            $result = apiCall(DatatreeApi::QUERY_NO_PAGING,[$map]);
            $countryHash = [];
            if($result['status']){
                foreach ($result['info'] as $val){
                    $countryHash[$val['code']] = $val['name'];
                }
            }
            foreach($list as &$val){
                $val['img'] = $PICTURE_REMOTE_URL.$val['img'].'&size=120';
                $val['country'] = isset($countryHash[$val['dt_origin_country']])?$countryHash[$val['dt_origin_country']]:'';
                if($group_id){
                    //获取group 最低价
                    $map = ['g_id'=>$group_id,'p_id'=>$val['product_id']];
                    $field = "MIN(price) as min_price";
                    $result = apiCall(ProductGroupApi::QUERY_NO_PAGING,[$map,false,$field]);
                    $min_price = $result['info'][0]['min_price'];
                    if($result['status'] && !is_null($min_price)){
                        $val['group_price'] = $min_price;
                    }else{
                        $val['group_price'] = $val['price'];
                    }
                }else{

                }

            }
            if(is_null($list)){
                $list = [];
            }
            return ['status'=>true,'info'=>['count'=>$count,'list'=>$list]];
        }else{
            return ['status'=>false,'info'=>'error'];
        }

    }

    /**
     *下拉刷新
     */
    public function queryProduct(){
        if(IS_POST){
            $curpage = I('post.curpage',1);
            $size = I('post.size',10);
            $ids = I('post.ids');
            $group_id = I('group_id',false);

            $result = $this->_queryProduct($ids,$curpage,$size,$group_id);
            if($result['status']){
                $this->ajaxReturnSuc($result['info']);
            }else{
                $this->ajaxReturnErr($result['info']);
            }
        }
    }

    /**
     * 搭配购下拉刷新
     */
    public function querydpg(){

        if(IS_POST){
            //搭配购
            //搭配购商品列表 套餐dt_type == 1;
            $curpage = I('post.curpage',1);
            $size = I('post.size',10);
            $map = ['dt_type' => 1];
            $page = ['curpage'=>$curpage, 'size'=>$size];
            $result = apiCall(ProductPackageApi::QUERY_WITH_COUNT,[$map,$page,false]);
            if($result['status']){
                //查询套餐信息
                $info = $result['info'];
                if(is_null($info['list'])){
                    $info['list'] = [];
                }else{
                    foreach($info['list'] as &$val){
                        $result = apiCall(ProductPackageApi::PACKAGE_DETAIL,[$val['id']]);
                        if($result['status']){
                            $val['detail'] = $result['info'];
                        }
                    }
                }

                $this->ajaxReturnSuc($info);
            }else{
                $this->ajaxReturnErr($result['info']);
            }
        }
    }

    private function ajaxReturnErr($info='error'){
        $this->ajaxReturn(['status'=>false,'info'=>$info]);
    }

    private function ajaxReturnSuc($info='success'){
        $this->ajaxReturn(['status'=>true,'info'=>$info]);
    }

}